<template>
  <d2-container>
    <div style="background-color: rgba(255, 255, 255, 0.9);">
      <div class="formsize1">
        <el-descriptions title="考核单号">
          <el-descriptions-item label="考核名称">{{
          examineelist.appraisalTemplate.templateName
        }}</el-descriptions-item>
          <el-descriptions-item label="创建人">{{
          examineelist.creator
        }}</el-descriptions-item>
          <el-descriptions-item label="打分类型">逐一打分</el-descriptions-item>
          <el-descriptions-item label="考核类型">{{
          examineelist.appraisalTemplate.type
        }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{
          examineelist.createTime
        }}</el-descriptions-item>
          <el-descriptions-item label="分值">100分制</el-descriptions-item>
          <el-descriptions-item label="被考核人">{{
          examineelist.name
        }}</el-descriptions-item>
          <el-descriptions-item label="得分">{{
         SubmitExaminelist.score
        }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <div class="progress">
        <el-steps :active="this.examineelist.status" align-center>
          <el-step title="待考核" description="待考核"></el-step>
          <el-step title="待审核" description="待审核"></el-step>
          <el-step title="已完成" description="已完成不可再提交"></el-step>
        </el-steps>
      </div>
      <div class="details">
        <el-table :data="tableData1" style="width: 100%" :row-class-name="tableRowClassName">
          <el-table-column prop="assessItem.ifpaName" label="指标名称" width="180">
          </el-table-column>
          <el-table-column prop="assessItem.name" label="考核项" width="180">
          </el-table-column>
          <el-table-column prop="assessItem.targetedValue" label="评分值"> </el-table-column>
          <el-table-column prop="p1" label="人均占比"> </el-table-column>
          <el-table-column prop="assessItem.assessStandard" label="评分方式"> </el-table-column>
          <el-table-column prop="score" label="评分">
            <!-- <input type="text" :value="score" style="width:50px;" /> -->
          </el-table-column>
        </el-table>
        评语<br>
        <input style="width:800px;height:80px;" type="text" v-model="SubmitExaminelist.comment" />
      </div>
      <center>
        <div class="button">
          <el-button type="primary" @click="submit" :disabled="ButtonStatus">提交</el-button>
          <!-- <el-button type="primary">重填</el-button> -->
          <el-button @click="returnexaminelist" type="primary">返回</el-button>
        </div>
      </center>
    </div>
  </d2-container>
</template>
<style>
  .details {
    background-color: white;
    width: 70%;
    height: 90%;
    margin: auto;
    padding-bottom: 20px;
  }

  .progress {
    background-color: white;
    width: 70%;
    height: 90%;
    height: 60px;
    margin-left: 150px;
    padding-bottom: 20px;
  }

  .button {
    background-color: white;
    width: 70%;
  }

  .formsize1 {
    background-color: white;
    width: 70%;
    height: 100%;
    margin: auto;
    padding-bottom: 10px;
  }

  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>

<script>
export default {
  data () {
    return {
      SubmitExaminelist: {
        id: '',
        comment: '请书写评语',
        score: '未结束评分',
        status: ''
      },
      ButtonStatus0: false,
      ButtonStatus: false,
      tableData: [],
      tableData1: [],
      tableData2: [],
      tableData3: [],
      examineListId: 0,
      examineelist: {}
    }
  },
  created () {
    this.examineListId = this.$route.query.eid
    if (this.examineListId > 0) {
      this.getdata()
    }
  },
  methods: {
    tableRowClassName ({
      row,
      rowIndex
    }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },
    // 查询一条部门考核列表信息方法
    getdata () {
      this.$request({
        url: 'examineList/findOneExamineList',
        method: 'get',
        params: {
          id: this.examineListId
        }
      }).then(res => {
        this.examineelist = res
        this.SubmitExaminelist.id = res.id
        this.statuslist()
        this.tableData = res.appraisalTemplate.templateAssess
        // 封装数据
        var a = 0
        var scorereal = 0
        var arr0 = []
        // 遍历得一个考核详情表对象
        for (const key in this.examineelist.list) {
          if (Object.hasOwnProperty.call(this.examineelist.list, key)) {
            const element = this.examineelist.list[key]
            // 遍历得一个实际打分表对象
            for (const key1 in element.rater) {
              if (Object.hasOwnProperty.call(element.rater, key1)) {
                const element1 = element.rater[key1]
                // 遍历得一个模板和考核项中间表对象
                for (const key0 in this.examineelist.appraisalTemplate.templateAssess) {
                  if (Object.hasOwnProperty.call(this.examineelist.appraisalTemplate
                    .templateAssess, key0)) {
                    const element0 = this.examineelist.appraisalTemplate.templateAssess[
                      key0]
                    if (element0.assessId === element.assessId) {
                      element1.assessItem = element0.assessItem
                      element1.p1 = element1.proportion
                      scorereal = scorereal + element1.p1 * element1.score
                    }
                  }
                }
                arr0[a] = element1
                this.tableData1[a] = arr0[a]
                a++
              }
            }
          }
        }
        var scorenum = 0
        for (const key in this.tableData1) {
          if (Object.hasOwnProperty.call(this.tableData1, key)) {
            const element = this.tableData1[key].status
            if (element !== 1) {
              this.ButtonStatus0 = true
              this.ButtonStatus = true
            } else {
              scorenum++
            }
          }
        }
        if (scorenum === this.tableData1.length && this.examineelist.status === 2) {
          this.ButtonStatus = false
          this.SubmitExaminelist.score = Math.round(scorereal * 10)
          this.SubmitExaminelist.status = '已完成'
        }
        if (this.examineelist.status === 3) {
          this.ButtonStatus = true
          this.SubmitExaminelist.score = this.examineelist.score
          this.SubmitExaminelist.comment = this.examineelist.comment
        }
      }).catch(err => {
        console.log(err)
      })
    },
    statuslist () {
      if (this.examineelist.status === '待考核') {
        this.examineelist.status = 1
      } else if (this.examineelist.status === '待审核') {
        this.examineelist.status = 2
      } else if (this.examineelist.status === '已完成') {
        this.examineelist.status = 3
      }
    },
    // 跳转新建模板页面
    returnexaminelist () {
      this.$router.push('/examinelist')
    },
    submit () {
      this.$request({
        url: 'examineList/updateStatus',
        method: 'post',
        data: this.SubmitExaminelist
      }).then(res => {
        alert('提交成功')
        this.$router.push('/examinelist')
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
